/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jsf;

import business.MD5Algorithm;
import data.Usertable;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.Principal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.validation.constraints.Size;

/**
 *
 * @author stana
 */

@ManagedBean
@Stateless
public class ChangeAccountController {

    @Size(min=5,message="zadejte prosím minimálně 5 znaků")
    private String password;
    @Size(min=5,message="zadejte prosím minimálně 5 znaků")
    private String newPassword;
    @Size(min=5,message="zadejte prosím minimálně 5 znaků")
    private String checkPassword;
    private String info;
    @EJB
    private business.UsertableFacade userFacade;
    Usertable user;

    public String getCheckPassword()
    {
        return checkPassword;
    }

    public void setCheckPassword(String checkPassword)
    {
        this.checkPassword = checkPassword;
    }

    public String getNewPassword()
    {
        return newPassword;
    }

    public void setNewPassword(String newPassword)
    {
        this.newPassword = newPassword;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public String getInfo()
    {
        return info;
    }

    public void setInfo(String info)
    {
        this.info = info;
    }



    public String update()
    {
        String md5="";
        Principal principal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
        if(!newPassword.equals(checkPassword))
        {
            info="Kontrola hesla nesouhlasí s novým heslem.";
            newPassword="";
            checkPassword="";
            return "";
        }
        if (principal != null)
        {
            user = userFacade.find(principal.getName()); // Find User by j_username.

        }
        try
        {
            md5 = MD5Algorithm.getMD5(password);
        } catch (NoSuchAlgorithmException ex)
        {
            Logger.getLogger(ChangeAccountController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex)
        {
            Logger.getLogger(ChangeAccountController.class.getName()).log(Level.SEVERE, null, ex);
        }
        if(!md5.equals(user.getPassword()))
        {
            info="Zadali jste chybné heslo.";
            password="";
            return "";
        }
        info="";
        try
        {
            user.setPassword(MD5Algorithm.getMD5(newPassword));
        } catch (NoSuchAlgorithmException ex)
        {
            Logger.getLogger(ChangeAccountController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex)
        {
            Logger.getLogger(ChangeAccountController.class.getName()).log(Level.SEVERE, null, ex);
        }
        userFacade.edit(user);
        return "/pages/user/CreateSuccess";
    }

}
